package com.ruoyi.business.controller;

import java.util.List;

import com.ruoyi.business.domain.vo.JtPurchaseOrderVo;
import com.ruoyi.business.service.IJtPurchaseOrderService;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.SysUserSupplier;
import com.ruoyi.business.service.ISysUserSupplierService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * 用户和供应商关联Controller
 * 
 * @author Alan Zhang
 * @date 2025-07-17
 */
@Controller
@RequestMapping("/business/user/supplier")
public class SysUserSupplierController extends BaseController
{
    private String prefix = "business/user/supplier";

    @Autowired
    private ISysUserSupplierService sysUserSupplierService;
    @Autowired
    private IJtPurchaseOrderService jtPurchaseOrderService;

    @RequiresPermissions("business:user:supplier:view")
    @GetMapping()
    public String supplier()
    {
        return prefix + "/supplier";
    }

    /**
     * 查询用户和供应商关联列表
     */
    @RequiresPermissions("business:user:supplier:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUserSupplier sysUserSupplier)
    {
        startPage();
        List<SysUserSupplier> list = sysUserSupplierService.selectSysUserSupplierList(sysUserSupplier);
        return getDataTable(list);
    }

    /**
     * 导出用户和供应商关联列表
     */
    @RequiresPermissions("business:user:supplier:export")
    @Log(title = "用户和供应商关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysUserSupplier sysUserSupplier)
    {
        List<SysUserSupplier> list = sysUserSupplierService.selectSysUserSupplierList(sysUserSupplier);
        ExcelUtil<SysUserSupplier> util = new ExcelUtil<SysUserSupplier>(SysUserSupplier.class);
        return util.exportExcel(list, "用户和供应商关联数据");
    }

    /**
     * 新增用户和供应商关联
     */
    @RequiresPermissions("business:user:supplier:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户和供应商关联
     */
    @RequiresPermissions("business:user:supplier:add")
    @Log(title = "用户和供应商关联", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysUserSupplier sysUserSupplier)
    {
        return toAjax(sysUserSupplierService.insertSysUserSupplier(sysUserSupplier));
    }

    /**
     * 修改用户和供应商关联
     */
    @RequiresPermissions("business:user:supplier:edit")
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        SysUserSupplier sysUserSupplier = sysUserSupplierService.selectSysUserSupplierByUserId(userId);
        mmap.put("sysUserSupplier", sysUserSupplier);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户和供应商关联
     */
    @RequiresPermissions("business:user:supplier:edit")
    @Log(title = "用户和供应商关联", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysUserSupplier sysUserSupplier)
    {
        return toAjax(sysUserSupplierService.updateSysUserSupplier(sysUserSupplier));
    }

    /**
     * 删除用户和供应商关联
     */
    @RequiresPermissions("business:user:supplier:remove")
    @Log(title = "用户和供应商关联", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysUserSupplierService.deleteSysUserSupplierByUserIds(ids));
    }

    /**
     * 绑定 用户和供应商关联
     */
    @RequiresPermissions("business:user:supplier:bind")
    @Log(title = "用户和供应商关联", businessType = BusinessType.INSERT)
    @PostMapping("/bind")
    @ResponseBody
    public AjaxResult bindSave(@NotEmpty String purchaseOrderNo)
    {
        JtPurchaseOrderVo jtPurchaseOrderVo = jtPurchaseOrderService.selectJtPurchaseOrderByPurchaseOrderNo(purchaseOrderNo);
        if(jtPurchaseOrderVo == null || jtPurchaseOrderVo.getSupplierId() == null){
            return AjaxResult.error("采购单未找到！或者该采购单没有供应商");
        }
        if("admin".equals(ShiroUtils.getLoginName())){
            return AjaxResult.error("不能绑定管理员用户");
        }
        SysUserSupplier sysUserSupplier = new SysUserSupplier();
        sysUserSupplier.setUserId(ShiroUtils.getUserId());
        sysUserSupplier.setSupplierId(jtPurchaseOrderVo.getPurchaseId());
        List<SysUserSupplier> list = sysUserSupplierService.selectSysUserSupplierList(sysUserSupplier);
        if(list.size() > 0){
            return AjaxResult.error("已绑定，请勿重复绑定！");
        }

        return toAjax(sysUserSupplierService.insertSysUserSupplier(sysUserSupplier));
    }
}
